package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.enums.LockFlagStatus;
import cn.b.sky.mes.mm.model.RequirementTriggerMode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.DistributionTriggerMode;
import cn.b.sky.mes.mm.model.PullStrategy;
import cn.b.sky.mes.mm.model.condition.PullStrategyCondition;
import cn.b.sky.mes.mm.service.DistributionTriggerModeService;
import cn.b.sky.mes.mm.service.PullStrategyService;
import cn.b.sky.mes.mm.service.RequirementTriggerModeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : pullStrategyController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.mm.controller<br></strong>
 * <strong>Description : </strong>拉动策略控制器<br> 
 * <strong>Create on : 2016-9-10 下午1:22:33<br></strong>
 * <p>
 * @author JinQuan.Fu<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/pullStrategy/*")
public class PullStrategyController extends SecurityController {

  @Resource
  PullStrategyService pullStrategyService;

  @Resource
  RequirementTriggerModeService requirementTriggerModeService;

  @Resource
  DistributionTriggerModeService distributionTriggerModeService;

  @RequestMapping("pullStrategy_list")
  public String pullStrategyListByCondition(String navTabId, Model model,
      @ModelAttribute("condition") PullStrategyCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setOrderField("CreateDateTime");
    List<PullStrategy> list = pullStrategyService.pullStrategyListByCondition(condition);
    int count = pullStrategyService.pullStrategyCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "mm/pullStrategy/pullStrategy_list";
  }

  @RequestMapping("viewPullStrategy")
  public String viewPullStrategy(String navTabId, Model model, String pullStrategyCode) {
    PullStrategy pullStrategy = pullStrategyService.getPullStrategy(getSiteCode(), pullStrategyCode);
    model.addAttribute("pullStrategy", pullStrategy);
    model.addAttribute(NAVTABID, navTabId);
    return "mm/pullStrategy/pullStrategy_view";
  }

  @RequestMapping("addPullStrategy")
  public String addPullStrategy(String navTabId, Model model) {
    List<DistributionTriggerMode> distributionTriggerModes = distributionTriggerModeService.selectDistributionTriggerModeByLockFlag(getSiteCode(), 
        new Integer(LockFlagStatus.ENABLE.getValue()));
    List<RequirementTriggerMode> requirementTriggerModes = requirementTriggerModeService.selectRequirementTriggerModeByLockFlag(getSiteCode(),
        new Integer(LockFlagStatus.ENABLE.getValue()));
    model.addAttribute("distributionTriggerModes", distributionTriggerModes);
    model.addAttribute("requirementTriggerModes", requirementTriggerModes);
    model.addAttribute(NAVTABID, navTabId);
    return "mm/pullStrategy/pullStrategy_add";
  }

  @RequestMapping("modifyPullStrategy")
  public String modifyPullStrategy(String navTabId, Model model, String pullStrategyCode) {
    PullStrategy pullStrategy = pullStrategyService.getPullStrategy(getSiteCode(), pullStrategyCode);
    List<DistributionTriggerMode> distributionTriggerModes = distributionTriggerModeService.selectDistributionTriggerModeByLockFlag(getSiteCode(), 
        new Integer(LockFlagStatus.ENABLE.getValue()));
    List<RequirementTriggerMode> requirementTriggerModes = requirementTriggerModeService.selectRequirementTriggerModeByLockFlag(getSiteCode(), 
        new Integer(LockFlagStatus.ENABLE.getValue()));
    model.addAttribute("distributionTriggerModes", distributionTriggerModes);
    model.addAttribute("requirementTriggerModes", requirementTriggerModes);
    model.addAttribute("pullStrategy", pullStrategy);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    return "mm/pullStrategy/pullStrategy_add";
  }

  @RequestMapping("validatePullStrategy")
  @ResponseBody
  public boolean validatePullStrategy(String pullStrategyCode) {
    if (StringUtils.isBlank(pullStrategyCode)) {
      return true;
    } else {
      PullStrategy pullStrategy = pullStrategyService.getPullStrategy(getSiteCode(), pullStrategyCode.toUpperCase());
      return pullStrategy == null;
    }
  }

  @RequestMapping("savePullStrategy")
  @ResponseBody
  public DWZResult savePullStrategy(String id, PullStrategy pullStrategy, String navTabId, Model model) {
    if (null == pullStrategy) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    pullStrategy.setSiteCode(this.getSiteCode());
    if (StringUtils.isBlank(id)) {
      setCreateInfo(pullStrategy);
      pullStrategyService.insertPullStrategy(pullStrategy);
    } else {
      setModifyInfo(pullStrategy);
      pullStrategyService.updatePullStrategy(pullStrategy);
    }
    return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), navTabId, null, null, null);
  }

  @RequestMapping("deletePullStrategy")
  @ResponseBody
  public DWZResult deletePullStrategy(String navTabId, Model model, String codes, HttpServletRequest request) {
    String[] idArr = codes.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String code = idArr[i];
      String message = null;
      message = pullStrategyService.detelePullStrategy(getSiteCode(), code);
      boolean flag = true;
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        flag = false;
        message = getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), message);
      }
      BatchJobReport report = new BatchJobReport(idArr[i], message, flag);
      reports.add(report);
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }

}
